# -*- coding: utf-8 -*-
"""
Created on Mon Aug 07 10:00:27 2017

@auth
"""

#%%
import logging
import os
import sys
import re
import time
import traceback
# from util import mysql
from logging.handlers import RotatingFileHandler

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
from selenium.common.exceptions import StaleElementReferenceException, NoSuchElementException, NoSuchWindowException
from base import BaiduMap

sys.path.append("../")

bizModule = "baidu_map_search"
logFileDir = "../log/%s/" % (bizModule)
if not os.path.exists(logFileDir):
    os.makedirs(logFileDir)
logger = logging.getLogger(bizModule)
hdlr = RotatingFileHandler(logFileDir + 'info.log', "a", 20 * 1024 * 1024, 20, "utf-8")
formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s')
hdlr.setFormatter(formatter)
logger.addHandler(hdlr)
logger.setLevel(logging.INFO)

class Search(BaiduMap):

    # def insert_db(self, line):
    #     # SQL 插入语句
    #     sql = "insert into potential_customer(create_time, update_time, uniqueid, contacts, company_name, company_url, job_title, phone, tel, fax, address, postcode,biztype) " \
    #           "values(%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)"
    #     # 执行sql语句
    #     result = None
    #     try:
    #         result = mysql.execute(sql, line)
    #     except Exception as e:
    #         print('str(Exception):\t', str(Exception))
    #         print('str(e):\t\t', str(e))
    #         print('repr(e):\t', repr(e))
    #         print('e.message:\t', e.message)
    #         print('traceback.print_exc():')
    #         traceback.print_exc()
    #         #print 'traceback.format_exc():\n%s' % traceback.format_exc()
    
    def get_total_page_count(self, total_page_count):
        return 100 if total_page_count > 100 else total_page_count
    
    def onepage_data(self):
        browser = self.browser
        smOfferList = browser.find_elements_by_xpath("//ul[@id='sm-offer-list']/li")
        smOfferListCount = len(smOfferList)
        for i in range(smOfferListCount):
            print(u"i: %d" % i)
            item = smOfferList[i]
            print(u"t-offer-id: %s" % item.get_attribute("t-offer-id"))
            companyPath = "div[@class='imgofferresult-mainBlock']/div[@class='s-widget-offershopwindowcompanyinfo sm-offer-company sw-dpl-offer-company']"
            company = item.find_element_by_xpath(companyPath)
            offerid = company.find_element_by_tag_name("a").get_attribute("offerid")
            title = company.find_element_by_tag_name("a").get_attribute("title")
            companyurl = company.find_element_by_tag_name("a").get_attribute("href")
            print(u"offerid: %s.title: %s.companyurl: %s" %(offerid,title,companyurl))
            
            companyurl = companyurl[0:companyurl.rfind(".1688")] + ".1688.com/"
            contactinfourl = companyurl + "page/contactinfo.htm?spm="
            # 打开详情链接
            ActionChains(browser).key_down(Keys.CONTROL).send_keys("t").key_up(Keys.CONTROL).perform()
            browser.switch_to_window(browser.window_handles[-1])
            try:
                browser.get(contactinfourl)
            except:
                browser.execute_script('window.stop()')
            #page_source = browser.page_source
            #browser.implicitly_wait(10)
            
            fieldDic = {}
            if not self.isElementExist("//div[@class='contact-info']/h4"):
                print(u"没有看到联系人")
            else:
                bizType = browser.find_element_by_xpath("//span[@class='biz-type-model']").text
                companyname = browser.find_element_by_xpath("//div[@class='contact-info']/h4").text
                membername = browser.find_element_by_xpath("//div[@class='contact-info']//dd/a[@class='membername']").text
                jobtitle = browser.find_element_by_xpath("//div[@class='contact-info']//dd").text
                jobtitle = re.sub(u'和我联系','',jobtitle)
                
                fieldDic[u"offerid"] = offerid
                fieldDic[u"店铺主页"] = companyurl
                fieldDic[u"公司名称"] = companyname
                fieldDic[u"联系人"] = membername
                fieldDic[u"职务"] = jobtitle
                fieldDic[u"生产模式"] = bizType
                if not self.isElementExist("//div[@class='contcat-desc']/dl"):
                    print(u"没有看到联系方式")
                else:
                    contcatdesc = browser.find_elements_by_xpath("//div[@class='contcat-desc']/dl")
                    for i in range(len(contcatdesc) - 1):
                        dlEle = contcatdesc[i]
                        field_name = dlEle.find_element_by_tag_name("dt").text
                        print(u"field_name: %s" % field_name)
                        field_name = field_name.rstrip(u"：")
                        field_name = re.sub('\s','',field_name)  #将string中的所有空白字符删除
                        field_data =  dlEle.find_element_by_tag_name("dd").text            
                        fieldDic[field_name] = field_data 
                for key in fieldDic:    
                    print("%s: %s" % (key, fieldDic[key]))
                curDate = time.strftime("%Y%m%d%H%M%S")#当前日期
                line_data = [curDate, curDate]
                line_data.append(fieldDic[u"offerid"] if fieldDic.has_key(u"offerid") else "")
                line_data.append(fieldDic[u"联系人"] if fieldDic.has_key(u"联系人") else "")
                line_data.append(fieldDic[u"公司名称"] if fieldDic.has_key(u"公司名称") else "")
                line_data.append(fieldDic[u"店铺主页"] if fieldDic.has_key(u"店铺主页") else "")
                line_data.append(fieldDic[u"职务"] if fieldDic.has_key(u"职务") else "")
                line_data.append(fieldDic[u"移动电话"] if fieldDic.has_key(u"移动电话") else "")
                line_data.append(fieldDic[u"电话"] if fieldDic.has_key(u"电话") else "")
                line_data.append(fieldDic[u"传真"] if fieldDic.has_key(u"传真") else "")
                line_data.append(fieldDic[u"地址"] if fieldDic.has_key(u"地址") else "")
                line_data.append(fieldDic[u"邮编"] if fieldDic.has_key(u"邮编") else "")
                line_data.append(fieldDic[u"生产模式"] if fieldDic.has_key(u"生产模式") else "")
                # self.insert_db(line_data)                    
            # 关闭标签页
            browser.close()
            browser.switch_to_window(browser.window_handles[-1])  
    
    def turnpage(self, search_word):
        browser = self.browser    
        #browser.get(search_word)
        browser.implicitly_wait(5)
        try:
            # 删除广告
            browser.find_element_by_xpath("//div[@class='close-btn-indexpage']").click()
            #browser.implicitly_wait(5)
        except:
            browser.execute_script('window.stop()')
          

    def crawl(self):
        search_word = "宠物"
        self.turnpage(search_word)
    
#%%                              
#==============================================================================
#                               登录
#==============================================================================
try:
    logger.info(u"开始登录")
    username = "lk240"   #第一个参数为用户名
    password = "aaaa"   #第二个参数为密码
    win = Search(username, password)
    if win.login():        
        print("登录成功")
        win.turnhandle()
    else:
        print("登录失败")
except Exception as e:
    logger.exception(u"登录出现异常")
finally:
    logger.info(u"登录结束")

#%%                              
#==============================================================================
#                               抓取商品
#==============================================================================
#新建标签页
win.crawl()
            





    